package cn.iocoder.yudao.module.famous.doctor.service.agreement;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.famous.doctor.controller.admin.agreement.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.agreement.AgreementDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.agreement.AgreementConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.agreement.AgreementMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 协议文档 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class AgreementServiceImpl implements AgreementService {

    @Resource
    private AgreementMapper agreementMapper;

    @Override
    public Long createAgreement(AgreementCreateReqVO createReqVO) {
        // 插入
        AgreementDO agreement = AgreementConvert.INSTANCE.convert(createReqVO);
        agreementMapper.insert(agreement);
        // 返回
        return agreement.getId();
    }

    @Override
    public void updateAgreement(AgreementUpdateReqVO updateReqVO) {
        // 校验存在
        validateAgreementExists(updateReqVO.getId());
        // 更新
        AgreementDO updateObj = AgreementConvert.INSTANCE.convert(updateReqVO);
        agreementMapper.updateById(updateObj);
    }

    @Override
    public void deleteAgreement(Long id) {
        // 校验存在
        validateAgreementExists(id);
        // 删除
        agreementMapper.deleteById(id);
    }

    private void validateAgreementExists(Long id) {
        if (agreementMapper.selectById(id) == null) {
            throw exception(AGREEMENT_NOT_EXISTS);
        }
    }

    @Override
    public AgreementDO getAgreement(Long id) {
        return agreementMapper.selectById(id);
    }

    @Override
    public List<AgreementDO> getAgreementList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return agreementMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AgreementDO> getAgreementPage(AgreementPageReqVO pageReqVO) {
        return agreementMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AgreementDO> getAgreementList(AgreementExportReqVO exportReqVO) {
        return agreementMapper.selectList(exportReqVO);
    }

}
